package com.mobile.ado.setting.ringtone;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.Editable;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.mobile.ado.R;

public class RingtoneSettingAddTimerDialog extends Dialog {

	private Activity activity;
	private Dialog dialog;
	private EditText startTimeEditText;
	private EditText endTimeEditText;

	private Button selectAudioButton;
	private TextView selectedAudioTextView;

	private String audioPath;

	public RingtoneSettingAddTimerDialog(Context context, int theme) {
		super(context, theme);
		this.activity = (Activity) context;
		dialog = this;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.setContentView(R.layout.ringtone_timer_add);

		WindowManager manager = activity.getWindow().getWindowManager();
		Display display = manager.getDefaultDisplay();

		WindowManager.LayoutParams params = getWindow().getAttributes();
		params.height = (int) (display.getHeight() * 0.6);
		params.width = (int) (display.getWidth() * 0.8);
		getWindow().setAttributes(
				(android.view.WindowManager.LayoutParams) params);

		startTimeEditText = (EditText) findViewById(R.id.fromText);
		endTimeEditText = (EditText) findViewById(R.id.toText);

		setupAudioSelectButton();

		selectedAudioTextView = (TextView) findViewById(R.id.video_path_textview);

		setupOkButton();
		setupCancelButton();

	}

	private void setupAudioSelectButton() {
		selectAudioButton = (Button) findViewById(R.id.audoPathSelect);
	}

	private void setupOkButton() {
		Button okButton = (Button) findViewById(R.id.okButton_ringtone);
		okButton.setOnClickListener(new android.view.View.OnClickListener() {

			@Override
			public void onClick(View view) {
				SharedPreferences preferences = dialog
						.getContext()
						.getSharedPreferences("ringtones", Context.MODE_PRIVATE);
				Editable startTime = startTimeEditText.getText();
				String startTimeString = "";
				if (startTime != null) {
					startTimeString = startTime.toString().trim();
				}

				Editable endTime = endTimeEditText.getText();
				String endTimeString = "";
				if (endTime != null) {
					endTimeString = endTime.toString().trim();
				}

				String value = startTimeString + ";" + endTimeString + ";"
						+ audioPath;

				Editor editor = preferences.edit();
				editor.putString(audioPath, value);
				editor.commit();

			}

		});
	}

	private void setupCancelButton() {
		Button cancelButton = (Button) findViewById(R.id.cancelButton_ringtone);
		cancelButton
				.setOnClickListener(new android.view.View.OnClickListener() {

					@Override
					public void onClick(View view) {
						dialog.cancel();
					}
				});
	}
}
